<?php
/**************************************
 *
 * @services 服务层~主要进行数据筛选与过滤
 *
 **************************************/
namespace app\common\services;

use app\common\repositories\RankingRepositories;
use GuzzleHttp\Client;
use think\config;
use think\Env;
use think\Validate;
use Green\Request\V20170112 as Green;

class RankingServices
{
    protected $RankingRepositories;

    protected $wechatSessionUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';

    public function __construct()
    {
        $this->RankingRepositories = new RankingRepositories();
    }
    /**
     * 利用code获取session_key以及openid
     *
     * @param $request
     * @return mixed
     */
    public function getWechatInfoByCode($request)
    {
        $fileName = Env::get('LOAD_CONFIG_APIKEY');
        $apikeyconfig = Config::load(APP_PATH.'/common/config/'.$fileName);
        $wxconfig = $apikeyconfig['xcxappkey'];
        $appId = $request['appid'];
        $url = sprintf($this->wechatSessionUrl, $wxconfig[$appId]['appId'], $wxconfig[$appId]['appSecret'], $request['code']);
        $client = new Client();
        $res = $client->request('GET', $url);
        // 获取状态码
        $code = $res->getStatusCode();
        if ($code != 200) {
            return false;
        } else {
            $res = $this->getWeChatApiResult($res->getBody());

            if (isset($res['errcode']) && isset($res['errmsg'])) {
                return false;
            }
            return $res;
        }
    }

    //排行榜
    public function weekList($param)
    {
        return $this->RankingRepositories->weekList($param);
    }

}